import requests
from lxml import etree
if __name__ == "__main__":

#创建session对象
    session = requests.Session()

# 爬取页面源码数据
    headers = {
        'user-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
    page_text = requests.get(url=url,headers=headers).text
    tree = etree.HTML(page_text)
    code_img_src = 'https://so.gushiwen.cn'+tree.xpath('//*[@id="imgCode"]/@src')[0]
    img_data = session.get(url=code_img_src,headers=headers).content
    with open ('./code.jpg','wb') as fp:
        fp.write(img_data)
        print('获取验证码成功！')

#识别验证码（手工）
#模拟登录

    login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
    data = {
        '__VIEWSTATE': 'fXXbyichFZ4p6duh/t6HNoGz51PAd/caYormlHOpbhLYKVEGyOaV/82d98R+FaGdxcHSgJoCeCGjxIORY0DH1T8ILH/xTHaj7AwFQ3eEfO0ddtLGGM2L0gHcavI=',
        '__VIEWSTATEGENERATOR': 'C93BE1AE',
        'from': 'http://so.gushiwen.cn/user/collect.aspx',
        'email': '13148374886',
        'pwd': 'war321',
        'code': input("请输入验证码 "),
        'denglu': '登录',
    }
# #手动cookie处理方法，不推荐（有的网站cookie会有时长限制）
#     headers = {
#         'Cookie': 'login=flase; ASP.NET_SessionId=mglpgqaro0nwkmtigfeu1lfk; Hm_lvt_9007fab6814e892d3020a64454da5a55=1616468592,1616477667,1616477675; codeyzgswso=ad68be51f2370798; Hm_lpvt_9007fab6814e892d3020a64454da5a55=1616498875; gsw2017user=1709982%7c425BA0284BC2A4902DBA401EB66E0239; login=flase; wxopenid=defoaltid; gswZhanghao=13148374886; gswPhone=13148374886'
#
#
#     }
    #使用session进行post请求发送
    login_page_text = session.post(url=login_url,headers=headers,data=data).text
    with open('gsw.html','w',encoding='utf-8') as fp:
        fp.write(login_page_text)
        print('输出成功！！')
